/***************************************************************************** 
** common/ioinit.c
** 
** Copyright (c) 2009~2014 ShangHai Infotm Ltd all rights reserved. 
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** Description: OEM functions.
**
** Author:
**     Warits   <warits.wang@infotm.com>
**      
** Revision History: 
** ----------------- 
** 1.1  XXX 03/24/2011 XXX	Warits
*****************************************************************************/

#include <common.h>
//#include <oem_func.h>
#include <asm/io.h>

struct oem_iolist {
	uint32_t	dat;
	uint32_t	bit;
	uint32_t	dir;
	uint32_t	val;
	uint32_t	dly;
};

int imapx200_ex_init_gpio(void)
{
	struct oem_iolist lst [] = CONFIG_SYS_EX_INIT_GPIO;
	uint32_t tmp, i;

	for(i = 0; lst[i].dat; i++)
	{
		tmp = readl(lst[i].dat + 4);
		tmp &= ~(3 << (lst[i].bit * 2));
		if(lst[i].dir)
		  /* in */
		  tmp |= (2 << (lst[i].bit * 2));
		else
		  /* out */
		  tmp |= (1 << (lst[i].bit * 2));

		writel(tmp, lst[i].dat + 4);

		if(!lst[i].dir)
		{
			tmp = readl(lst[i].dat);
			if(lst[i].val)
			  tmp |= (1 << lst[i].bit);
			else
			  tmp &= ~(1 << lst[i].bit);
			writel(tmp, lst[i].dat);
		}

		if(lst[i].dly)
		  udelay(lst[i].dly);
		
//		printf("IOINIT: 0x%08x, bit=%d, dir=%d, val=%d\n",
//		   lst[i].dat, lst[i].bit, lst[i].dir, lst[i].val);
	}

	return 0;
}

